package cn.org.timespace.easyframe.dao.permission.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Query;
import org.hibernate.Session;

import cn.org.timespace.easyframe.action.permission.EFUserModel;
import cn.org.timespace.easyframe.dao.EfBaseDAO;
import cn.org.timespace.easyframe.dao.permission.EfUserDao;
import cn.org.timespace.easyframe.model.permission.EfUser;

public class EfUserDaoImpl extends EfBaseDAO implements EfUserDao {

	public List<EfUser> findByCondition(EFUserModel model) {
		StringBuffer queryString = new StringBuffer("from EfUser a where 1=1");

		if (!StringUtils.isEmpty(model.getUserName())) {
			queryString.append(" and a.userName like '%" + model.getUserName()
					+ "%'");
		} 

		if (!StringUtils.isEmpty(model.getUserAccount())) {
			queryString.append(" and a.userAccount = '"
					+ model.getUserAccount() + "'");
		}

		if (!StringUtils.isEmpty(model.getDicUserPosition())) {
			queryString.append(" and a.dicUserPosition = '"
					+ model.getDicUserPosition() + "'");
		}

		if (!StringUtils.isEmpty(model.getDicUserIsValid())) {
			queryString.append(" and a.dicUserIsValid = '"
					+ model.getDicUserIsValid() + "'");
		}

		queryString.append(" and (dicUserIsValid='0' or dicUserIsValid='1') order by a.userId desc");

		return getHibernateTemplate().find(queryString.toString());
	}

	public void save(EfUser transientInstance) {
		log.debug("saving EfUser instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(EfUser persistentInstance) {
		log.debug("deleting EfUser instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public EfUser findById(java.lang.String userId) {
		log.debug("getting EfUser instance with id: " + userId);
		try {
			EfUser instance = (EfUser) getHibernateTemplate().get(
					"cn.org.timespace.easyframe.model.permission.EfUser", userId);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	/**
	 * 根据输入的用户名，密码进行验证 如果验证成功就返回该用户对象，如果验证失败就返回NULL值
	 */
	public EfUser authentication(EfUser model) {

		String queryString = "from EfUser a where a.userAccount = '"
				+ model.getUserAccount() + "' and a.userPassword = '"
				+ model.getUserPassword() + "'";

		List<EfUser> list = this.getHibernateTemplate().find(queryString);
		if (list != null && list.size() == 1) {
			return list.get(0);
		} else {
			return null;
		}

	}

	// 修改用户
	public void update(EfUser instance) {
		try {
			getHibernateTemplate().update(instance);
		} catch (RuntimeException re) {
			log.error("update failed", re);
			throw re;
		}
	}

	public EfUser findByUserAccount(String userAccount) {
		StringBuffer queryString = new StringBuffer("from EfUser a where a.userAccount = '"+userAccount+"'");
		List<EfUser> resList = getHibernateTemplate().find(queryString.toString());
		if(resList != null && resList.size() == 1){
			return resList.get(0);
		}
		return null;
	}
	
	public void updatePW(String userId, String newPW) {
		StringBuffer updateString = new StringBuffer("update EF_USER a set user_password = '"+newPW+"' where user_id = '"+userId+"' ");
		Session session = null;
		Query query = null;
		try {
			
			session = getSessionFactory().openSession();
			query = session.createSQLQuery(updateString.toString());
			query.executeUpdate();
			
		} catch (RuntimeException re) {
			log.error("updatePW failed", re);
			throw re;
		}finally{
			if(session != null) session.close();
			if(query != null) query = null;
		}
	}
	
}
